package com.shroo8.footballbloglib.dao.footballblogwrite;

import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.stereotype.Repository;

import com.shroo8.footballblog.dto.api.UserDto;
import com.shroo8.footballbloglib.dao.constants.DataAccessConstants;
import com.shroo8.footballbloglib.dao.util.DaoUtil;

@Repository(value="createUserDao")
public class CreateUserDao {

	//private static final Logger logger = LoggerFactory.getLogger(CreateUserDao.class);

	@Resource(name="dataSourceFootballblogWrite")
	protected DataSource dataSource;

	@Resource(name="daoUtil")
	protected DaoUtil daoUtil;

	protected final String sql = DataAccessConstants.SP_CREATE_USER; 
	
	public String execute(UserDto userDto){
		List<SqlParameter> sqlParameters = new ArrayList<SqlParameter>();
	    Map<String, Object> inParams = new HashMap<String, Object>();
		/*
		 * 	Parameter list:
			in_user_type ENUM('twitter','facebook'),
			in_user_external_id INT,
			in_user_screen_name VARCHAR(50),
			in_user_name VARCHAR(50),
			in_user_pic_url VARCHAR (100),
			in_user_big_pic_url VARCHAR (100),
			in_user_mini_pic_url VARCHAR (100),
			OUT out_user_id VARCHAR(36)
		*/
	    sqlParameters.add(new SqlParameter(DataAccessConstants.USERS_USER_TYPE, Types.CHAR));
        inParams.put(DataAccessConstants.USERS_USER_TYPE, userDto.getType());

	    sqlParameters.add(new SqlParameter(DataAccessConstants.USERS_USER_EXTERNAL_ID, Types.INTEGER));
        inParams.put(DataAccessConstants.USERS_USER_EXTERNAL_ID, userDto.getExternalId());

	    sqlParameters.add(new SqlParameter(DataAccessConstants.USERS_USER_SCREEN_NAME, Types.VARCHAR));
        inParams.put(DataAccessConstants.USERS_USER_SCREEN_NAME, userDto.getScreenName());

	    sqlParameters.add(new SqlParameter(DataAccessConstants.USERS_USER_NAME, Types.VARCHAR));
        inParams.put(DataAccessConstants.USERS_USER_NAME, userDto.getName());

	    sqlParameters.add(new SqlParameter(DataAccessConstants.USERS_USER_PIC_URL, Types.VARCHAR));
        inParams.put(DataAccessConstants.USERS_USER_PIC_URL, userDto.getPicUrl());

	    sqlParameters.add(new SqlParameter(DataAccessConstants.USERS_USER_BIG_PIC_URL, Types.VARCHAR));
        inParams.put(DataAccessConstants.USERS_USER_BIG_PIC_URL, userDto.getBigPicUrl());

	    sqlParameters.add(new SqlParameter(DataAccessConstants.USERS_USER_MINI_PIC_URL, Types.VARCHAR));
        inParams.put(DataAccessConstants.USERS_USER_MINI_PIC_URL, userDto.getMiniPicUrl());

        sqlParameters.add(new SqlOutParameter(DataAccessConstants.SP_CREATE_USER_OUT_PARAMETER, Types.VARCHAR));
	    
        Map<String, Object> out = daoUtil.spExecute(dataSource, sql,
                sqlParameters, inParams);

		return (String) out.get(DataAccessConstants.SP_CREATE_USER_OUT_PARAMETER);

	}
}
